// page/detail/pages/vote/vote.js
const utils = require('../../../../utils/util.js');
let myRequest = require('../../../../common/script/myRequest.js');
let api = require('../../../../common/script/api.js');
let login = require('../../../../common/script/login');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, // 页码
    pageSize: 10,
    title: '',
    periods: '',
    dataList: [],
    store_id: '24',
    isEnd: false,
    domain: api.http,
  },
  getData() {
    let that = this;
    let data = {
      token: wx.getStorageSync('token'),
      store_id: that.data.store_id,
      page: that.data.page,
      limit: that.data.pageSize,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.sacnQrcode, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          dataList: res.data.result.details,
          title: res.data.result.poll.title,
          periods: res.data.result.poll.periods
        })

      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
        })
      }
    })
  },
  //投票
  vote(e) {
    const that = this;
    const id = e.currentTarget.dataset.id;
    const index = e.currentTarget.dataset.index;
    wx.showLoading({
      title: '加载中...',
    })
    let data = {
      token: wx.getStorageSync('token'),
      detail_id: id,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.handle, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
        that.setData({
          ['dataList[' + index + '].is_can']: 0
        })
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    let sceneObj = utils.parseQueryString(options.scene)
    that.setData({
      store_id: sceneObj.store_id,
      // store_id: 24
    })
    login.checkLogin().then(res => {
      that.getData()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData()
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      let page = that.data.page;
      page++;
      let data = {
        token: wx.getStorageSync('token'),
        store_id: that.data.store_id,
        page,
        limit: that.data.pageSize,
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.sacnQrcode, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          if (res.data.result.details.length == 0) {
            that.setData({
              isEnd: true
            })
          } else {
            let dataList = [...that.data.dataList, ...res.data.result.details]
            that.setData({
              page,
              dataList
            })
          }
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})